<?php
// Offkai Model
// $Id: Offkai.php 2 2009-12-25 09:19:58Z mocapapa@g.pugpug.org $
class Offkai extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Offkai':
	 * @var string $offkaicode
	 * @var string $meishou
	 * @var string $password
	 * @var string $kaisaibi
	 * @var string $basho
	 * @var string $kanji
	 * @var string $joutai
	 * @var string $gaiyou
	 * @var string $modified
	 */

	public $authcode;
	public $dbpassword;

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Offkai';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('meishou, offkaicode, password, basho, kanji, joutai, gaiyou', 'required', 'on'=>'insert, update'),
			array('offkaicode', 'required', 'on'=>'add_check'),
			array('offkaicode', 'unique', 'on'=>'add_check'),
			array('offkaicode', 'required', 'on'=>'menu'),
			array('offkaicode', 'exist', 'on'=>'menu'),
			array('password', 'authenticate', 'on'=>'update'),
			array('authcode', 'captcha', 'on'=>'insert, update', 'allowEmpty'=>!extension_loaded('gd')),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			'sankasya' => array(self::HAS_MANY, 'Sankasya', 'offkaicode',
				'order'=>'??.modified DESC'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'offkaicode'=>'オフ会コード',
			'meishou'=>'オフ会名称',
			'password'=>'パスワード',
			'kaisaibi'=>'開催日',
			'basho'=>'場所',
			'kanji'=>'幹事',
			'joutai'=>'状態',
			'gaiyou'=>'概要',
			'authimage'=>'認証画像',
			'authcode'=>'認証コード',
		);
	}

        public function safeAttributes()
        {
		return array(
			// any scenario
			'meishou, offkaicode, password, kaisaibi, basho, kanji, joutai, gaiyou, authcode',
		);
        }

	/**
	 *  Password validator
	 */
	public function authenticate() {
        	if ($this->password != $this->dbpassword) {
			$this->addError('password','パスワードが違います。');
		}
	}

	/**
	 *  Convert a date format
	 */
	public static function toJtime( $time ) {
		$youbiarray = array(
			'1' => '月',
			'2' => '火',
			'3' => '水',
			'4' => '木',
			'5' => '金',
			'6' => '土',
			'7' => '日');
		$year   = substr( $time, 0, 4 );
		$month  = substr( $time, 5, 2 );
		$day    = substr( $time, 8, 2 );
		$mtime = mktime(0,0,0,$month,$day,$year);
		$youbi = strftime("%u", $mtime);
		return "${year}年${month}月${day}日($youbiarray[$youbi])";
	}
}
